package com.imay.live.fragment.discovery;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.live.R;
import com.imay.live.activity.RoomLiveNormalActivity;
import com.imay.live.adapter.GameLiveAdapter;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.model.RoomInfoModel;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.view.custom.GameTitleView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * Created by liangzhenxiong on 2017/4/7 0006.
 * 发现-游戏
 */

public class GameFragment extends LoadFragment {
    public static final int DATACOUNT = 20;

    public static GameFragment newInstance() {
        return new GameFragment();
    }


    @Override
    public void setTabH(int tabH) {
        super.setTabH(tabH);
    }

    @Override
    public boolean getsetEnableRefresh() {
        if (mLinearRecyclerView != null) {
            RecyclerView.LayoutManager layoutManager = mLinearRecyclerView.getLayoutManager();
            if (layoutManager instanceof LinearLayoutManager) {
                int index = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
                return index == 0;
            }
        }
        return super.getsetEnableRefresh();
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        try {
            Result<ArrayList<RoomInfoModel>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<RoomInfoModel>>>() {
            }.getType());
            if (result.result == CodeSuccess && result.data != null && result.data.size() > 0) {
                mpapecount = result.data.size();
                mAdapter.setNewData(result.data);
                addFooterView(tabH);
                completeLoad(mpapecount == 0);
            } else {
                completeLoad(true);
                loadError(getResources().getString(R.string.str_loaderror));
            }

        } catch (Exception e) {
            loadError(getResources().getString(R.string.str_loaderror));
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        loadError(getResources().getString(R.string.str_loaderror));
    }

//    public void onRefresh() {
//        ProtoRequestHelper.task(ProtoHttpFactory.getRoomingames(0, DATACOUNT)).setTag(this.hashCode()).request(new ProtoCallBack() {
//            @Override
//            public void onRequestSuccess(String command, String data) {
//                GameFragment.this.onRequestSuccess(command, data);
//            }
//
//            @Override
//            public void onRequestError(String command, int code, String message) {
//                GameFragment.this.onRequestError(command, code, message);
//            }
//        });
//    }

    @Override
    public void onLoadMore() {
        ProtoRequestHelper.task(ProtoHttpFactory.getRoomingames(mpapecount, DATACOUNT)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {

                try {
                    Result<ArrayList<RoomInfoModel>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<RoomInfoModel>>>() {
                    }.getType());
                    if (result.result == CodeSuccess && result.data != null && result.data.size() > 0) {
                        mpapecount = result.data.size() + mpapecount;
                        mAdapter.addData(result.data);
                        completeLoadmore(result.data.size() == 0);
                    } else {
                        completeLoadmore(true);
                        loadmoreError(getResources().getString(R.string.str_loaderror));
                    }

                } catch (Exception e) {
                    loadmoreError(getResources().getString(R.string.str_loaderror));
                }
            }

            @Override
            public void onRequestError(String command, int code, String message) {
                loadmoreError(getResources().getString(R.string.str_loaderror));
            }
        });
    }

    @Override
    void initAdapte() {
        needmore = true;
        mLinearRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mAdapter = new GameLiveAdapter(null);
        mLinearRecyclerView.setAdapter(mAdapter);
        mLinearRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .size(0)
                .build());
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.discover_live_rl_square) {
                    RoomLiveNormalActivity.startActivity(getActivity(), (ArrayList<RoomInfoModel>) adapter.getData(), position);
                }
//                else if (view.getId() == R.id.discover_live_tv_nick) {
//                    RoomInfoModel roomInfoModel = (RoomInfoModel) adapter.getItem(position);
//                    if (roomInfoModel.getUser() != null) {
//                        ProfileActivity.start(getActivity(), roomInfoModel.getUser().getUid());
//                    }
//                }
            }
        });
    }



    @Override
    void oncreate() {
//        mLinearRecyclerView.setBackgroundColor(getResources().getColor(R.color.bg_f5));
        gameTitleTextView = new GameTitleView(getActivity());
        gameTitleTextView.bindView(getActivity(),null);
        mAdapter.addHeaderView(gameTitleTextView);
        MobclickAgent.onEvent(getActivity(), "find_game");
    }

}
